package cn.example.vip.mall.goods.service.impl;

import cn.example.vip.mall.cart.entity.Cart;
import cn.example.vip.mall.goods.entity.AdItems;
import cn.example.vip.mall.goods.entity.Sku;
import cn.example.vip.mall.goods.mapper.AdItemsMapper;
import cn.example.vip.mall.goods.mapper.SkuMapper;
import cn.example.vip.mall.goods.service.SkuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@CacheConfig(cacheNames = "ad-items-skus")
@Service
public class SkuServiceImpl extends ServiceImpl<SkuMapper, Sku> implements SkuService {
	@Autowired
	SkuMapper skuMapper;
	@Autowired
	AdItemsMapper adItemsMapper;
	
	/**
	 * 根据type查询adItem并删除缓存
	 * @param type
	 * @return
	 */
	@Cacheable(key = "#type")	// 根据adItems的SkuId查询Sku
	@Override
	public List<Sku> typeSkuItems(Integer type) {
		System.out.println("查询数据库！");
		QueryWrapper<AdItems> adItemsQueryWrapper = new QueryWrapper<>();
		// 根据adItems列表的sku_id列表
		adItemsQueryWrapper.eq("type", type);
		List<AdItems> adItemsList = adItemsMapper.selectList(adItemsQueryWrapper);
		System.out.println("查询Items----------" + adItemsList);
		// 根据sku_id列表查询产品列表信息
		List<String> skuIds = adItemsList.stream().map(AdItems::getSkuId).collect(Collectors.toList()); // (不是哥们？你用string查? 效率还不如写个循环呢)
		return skuMapper.selectBatchIds(skuIds);
	}
	
	@CacheEvict(key = "#type")	// 根据adItems的SkuId查询Sku
	@Override
	public void delTypeSkuItems(Integer type) {
//		adItemsMapper.deleteById(type);
	}
	
	/**
	 * 根据type查询adItem并更新缓存
	 * @param type
	 * @return
	 */
	@CachePut(key = "#type")	// 根据adItems的SkuId查询Sku
	@Override
	public List<Sku> updateTypeSkuItems(Integer type) {
		System.out.println("查询数据库！");
		QueryWrapper<AdItems> adItemsQueryWrapper = new QueryWrapper<>();
		// 根据adItems列表的sku_id列表
		adItemsQueryWrapper.eq("type", type);
		List<AdItems> adItemsList = adItemsMapper.selectList(adItemsQueryWrapper);
		System.out.println("查询Items----------" + adItemsList);
		// 根据sku_id列表查询产品列表信息
		List<String> skuids = adItemsList.stream().map(AdItems::getSkuId).collect(Collectors.toList());
		return skuMapper.selectBatchIds(skuids);
	}
	
	/**
	 * 减库存
	 * @param carts
	 */
	@GlobalTransactional(rollbackFor = Exception.class)
	@Transactional
	@Override
	public void dCount(List<Cart> carts) {
		System.err.println("carts-------->" + carts);
		for(Cart cart : carts){
			// 减库存
			int dCounts = skuMapper.dCount(cart.getSkuId(), cart.getNum());
			int i = 10/0;	// 异常回滚测试
			if(dCounts <= 0){
				throw new RuntimeException("库存不足!");
			}
		}
	}
	
}
